package cn.sc.summer.feign.retry;

import feign.RetryableException;
import feign.Retryer;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 类名：自定义超时重试类
 *
 * @author a-xin
 * @date 2023/12/8 14:13
 */
@Slf4j
@AllArgsConstructor
public class RetryerConfig implements Retryer {

    /**
     * 最大尝试次数
     */
    private int maxAttempts;

    /**
     * 重试间隔时间
     */
    private long backoff;

    /**
     * 当前重试次数
     */
    private int attempt;

    @Override
    public void continueOrPropagate(RetryableException e) {
        if (attempt++ >= maxAttempts) {
            throw e;
        }
        log.error("==> Openfeign Retry frequency: {}, Retry interval: {}", attempt, backoff);
    }

    @Override
    public Retryer clone() {
        return this;
    }

}
